<template>
  <div class="info">
    <el-form>
      <el-form-item label="头像">
        <div>
          <el-upload action="http://192.168.137.35:8080/api/upload" :show-file-list="false" :on-success="handleAvatarSuccess">
            <img v-if="form.avatar" :src="`http://192.168.137.35:8080/files/${form.avatar}`" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </div>
      </el-form-item>
      <el-form-item label="昵称">
        <el-input v-model="form.nickname"></el-input>
      </el-form-item>
      <el-form-item label="个性签名">
        <el-input v-model="form.message"></el-input>
      </el-form-item>
    </el-form>

    <div>
      <el-button type="primary" @click="updateInfo">更新</el-button>
      <el-button type="primary" @click="back">回首页</el-button>
    </div>
  </div>
</template>

<script>
import {
  Input,
  Form,
  FormItem,
  Upload,
  Button,
} from 'element-ui';
import axios from '../../helper/axios';

export default {
  name: 'Info',
  components: {
    'el-input': Input,
    'el-form': Form,
    'el-form-item': FormItem,
    'el-upload': Upload,
    'el-button': Button,
  },
  data() {
    return {
      form: {
        avatar: '',
        nickname: '',
        message: '',
      },
    };
  },
  mounted() {
    axios.get('/api/user/detail').then((res) => {
      this.form = res;
    });
  },
  methods: {
    handleAvatarSuccess(res) {
      // eslint-disable-next-line
      this.form.avatar = res.data[0];
    },
    updateInfo() {
      axios.post('/api/user/update', this.form);
    },
    back() {
      this.$router.replace('/');
    },
  },
};
</script>

<style>
.info {
  width: 100vw;
  padding-top: 50px;
  padding-left: 100px;
}
.el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 108px;
  height: 108px;
  line-height: 108px;
  text-align: center;
}
.avatar {
  width: 108px;
  height: 108px;
  display: block;
}
</style>
